/*
 * Copyright (C) 2008  Young,Fey <fey.young@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ 
package com.youngfey.wordmaster.database.tables

import com.youngfey.database.annotation.*
import com.youngfey.wordmaster.database.*
import static com.youngfey.wordmaster.database.Database.*

public class Review {
    Sentence sentence

    Date reviewDate

    @DefaultValue('0')
    Integer effect

    // <editor-fold defaultstate="collapsed" desc=" Generated Code (by gRDB)">
    Integer id
    

    boolean equals(Object o) {
        id != null && o instanceof Review && id == o.getId()
    }

    static createTable() {
        execute("CREATE TABLE Review (Id INT NOT NULL GENERATED ALWAYS AS IDENTITY CONSTRAINT REVIEW_PK PRIMARY KEY, Sentence INT NOT NULL, ReviewDate INT NOT NULL, Effect INT DEFAULT 0 NOT NULL)")
        
    }

    boolean isValid() {
        sentence != null && reviewDate != null && effect != null
    }



    static Review create(sentence, reviewDate, effect) {
        def instance = new Review(sentence: sentence, reviewDate: reviewDate, effect: effect)
        instance.insert()
        instance
    }

    def insert() {
        if(id == null && isValid()) {
            id = executeInsert("INSERT INTO Review (Sentence, ReviewDate, Effect) VALUES (${sentence?.getId()}, ${reviewDate?.getId()}, ${effect})")[0][0]
            if(id != null) {
                
                return
            }
        }

        throw new IllegalStateException()
    }

    //capitalize;
    static Review find(Integer id) {
        if(id == null) return null

        

        def row = firstRow("SELECT * FROM Review WHERE Id = $id")
        
        row ? new Review(id: row.Id, sentence: Sentence.find((Short)row.sentence), reviewDate: Date.find((Short)row.reviewDate), effect: row.effect) : null
        
    }

    static ArrayList<Review> findBySql(stmt) {
        def rows = []
        eachRow(stmt) {row ->
            rows << new Review(id: row.Id, sentence: Sentence.find((Short)row.sentence), reviewDate: Date.find((Short)row.reviewDate), effect: row.effect)
        }
        
        rows
    }

    static ArrayList<Review> findAll() {
        findBySql('SELECT * FROM Review')
    }

    

    private executeUpdate(stmt) {
        def updated = executeUpdate(stmt)
        if(updated != 1) throw new IllegalStateException()
        
    }

    def update() {
        if(id != null && isValid()) {
            executeUpdate("UPDATE Review SET Sentence = ${sentence?.getId()}, ReviewDate = ${reviewDate?.getId()}, Effect = ${effect} WHERE Id = $id")
        }
    }

    static delete(Integer id) {
        execute("DELETE FROM Review WHERE Id = $id")
        
    }

    def delete() {
        if(id != null) delete(id)
    }


    static ArrayList<Review> findBySentence(value) {
        findBySql("SELECT * FROM Review WHERE Sentence = ${value?.getId()}")
    }

    static ArrayList<Review> findByReviewDate(value) {
        findBySql("SELECT * FROM Review WHERE ReviewDate = ${value?.getId()}")
    }

    static ArrayList<Review> findByEffect(value) {
        findBySql("SELECT * FROM Review WHERE Effect = ${value}")
    }



    static ArrayList<Review> findBySentenceLike(String keyword) {
        findBySql("SELECT * FROM Review WHERE Sentence LIKE $keyword")
    }

    static ArrayList<Review> findByReviewDateLike(String keyword) {
        findBySql("SELECT * FROM Review WHERE ReviewDate LIKE $keyword")
    }

    static ArrayList<Review> findByEffectLike(String keyword) {
        findBySql("SELECT * FROM Review WHERE Effect LIKE $keyword")
    }



    def updateSentence(value) {
        this.sentence = value

        if(id != null && isValid()) {
            executeUpdate("UPDATE Review SET Sentence = ${value?.getId()} WHERE Id = $id")
        } else {
            throw new IllegalStateException("Update Error: value = $value")
        }
    }

    def updateReviewDate(value) {
        this.reviewDate = value

        if(id != null && isValid()) {
            executeUpdate("UPDATE Review SET ReviewDate = ${value?.getId()} WHERE Id = $id")
        } else {
            throw new IllegalStateException("Update Error: value = $value")
        }
    }

    def updateEffect(value) {
        this.effect = value

        if(id != null && isValid()) {
            executeUpdate("UPDATE Review SET Effect = ${value} WHERE Id = $id")
        } else {
            throw new IllegalStateException("Update Error: value = $value")
        }
    }



    // </editor-fold>
}
